---
layout: "default"
title: "SetAlgebra"
description: "Swift documentation for 'SetAlgebra': A type that provides mathematical set operations."
keywords: "SetAlgebra,protocol,swift,documentation,==,contains,formIntersection,formSymmetricDifference,formUnion,insert,intersection,isDisjoint,isSubset,isSuperset,remove,subtract,subtracting,symmetricDifference,union,update,isEmpty,Element"
root: "/v3.0"
---

<div class="intro-declaration"><code class="language-swift">protocol SetAlgebra</code></div>

<div class="discussion comment">
    <p>A type that provides mathematical set operations.</p>

<p>You use types that conform to the <code>SetAlgebra</code> protocol when you need
efficient membership tests or mathematical set operations such as
intersection, union, and subtraction. In the standard library, you can
use the <code>Set</code> type with elements of any hashable type, or you can easily
create bit masks with <code>SetAlgebra</code> conformance using the <code>OptionSet</code>
protocol. See those types for more information.</p>

<p><strong>Note:</strong> Unlike ordinary set types, the <code>Element</code> type of an <code>OptionSet</code> is
  identical to the <code>OptionSet</code> type itself. The <code>SetAlgebra</code> protocol is
  specifically designed to accommodate both kinds of set.</p>

<h1>Conforming to the SetAlgebra Protocol</h1>

<p>When implementing a custom type that conforms to the <code>SetAlgebra</code> protocol,
you must implement the required initializers and methods. For the
inherited methods to work properly, conforming types must meet the
following axioms. Assume that <code>S</code> is a custom type that conforms to the
<code>SetAlgebra</code> protocol, <code>x</code> and <code>y</code> are instances of <code>S</code>, and <code>e</code> is of
type <code>S.Element</code>---the type that the set holds.</p>

<ul><li><code>S() == []</code></li><li><code>x.intersection(x) == x</code></li><li><code>x.intersection([]) == []</code></li><li><code>x.union(x) == x</code></li><li><code>x.union([]) == x</code></li><li><code>x.contains(e)</code> implies <code>x.union(y).contains(e)</code></li><li><code>x.union(y).contains(e)</code> implies <code>x.contains(e) || y.contains(e)</code></li><li><code>x.contains(e) &amp;&amp; y.contains(e)</code> if and only if
<code>x.intersection(y).contains(e)</code>
<strong><code>x.isSubset(of:** y)</code> if and only if <code>y.isSuperset(of: x)</code>
</strong><code>x.isStrictSuperset(of:** y)</code> if and only if
<code>x.isSuperset(of: y) &amp;&amp; x != y</code>
**<code>x.isStrictSubset(of:** y)</code> if and only if <code>x.isSubset(of: y) &amp;&amp; x != y</code></li></ul>

<p><strong>See Also:</strong> <code>OptionSet</code>, <code>Set</code></p>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">Equatable, ExpressibleByArrayLiteral</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>

<tr>
<th id="aliases">Associated Types</th>
<td>
<span id="aliasesmark"></span>
<div class="declaration">
<code class="language-swift">Element</code>
<div class="comment">
    <p>A type for which the conforming type provides a containment test.</p>
</div>
</div>
</td>
</tr>


<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>


<h3>Initializers</h3>
<div class="declaration" id="init">
<a class="toggle-link" data-toggle="collapse" href="#comment-init">init()</a> <span class="required">Required</span><div class="comment collapse" id="comment-init"><div class="p">
    <p>Creates an empty set.</p>

<p>This initializer is equivalent to initializing with an empty array
literal. For example, you create an empty <code>Set</code> instance with either
this initializer or with an empty array literal.</p>

<pre><code class="language-swift">var emptySet = Set&lt;Int&gt;()
print(emptySet.isEmpty)
// Prints &quot;true&quot;

emptySet = []
print(emptySet.isEmpty)
// Prints &quot;true&quot;</code></pre>

    <h4>Declaration</h4>    
    <code class="language-swift">init()</code>

    </div></div>
</div>
<div class="declaration" id="init_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_">init(<wbr>_:)</a><div class="comment collapse" id="comment-init_"><div class="p">
    <p>Creates a new set from a finite sequence of items.</p>

<p>Use this initializer to create a new set from an existing sequence, like
an array or a range:</p>

<pre><code class="language-swift">let validIndices = Set(0..&lt;7).subtracting([2, 4, 5])
print(validIndices)
// Prints &quot;[6, 0, 1, 3]&quot;</code></pre>

<p><strong><code>sequence</code>:</strong>  The elements to use as members of the new set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init&lt;S : Sequence where S.Iterator.Element == Element&gt;(_ sequence: S)</code>

    </div></div>
</div>
<div class="declaration inherited" id="init-arrayliteral_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-arrayliteral_">init(<wbr>arrayLiteral:)</a><div class="comment collapse" id="comment-init-arrayliteral_"><div class="p">
    <p>Creates an instance initialized with the given elements.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(arrayLiteral elements: Self.Element...)</code>

        <h4>Declared In</h4>
            <a href="../../protocol/ExpressibleByArrayLiteral/"><code>ExpressibleByArrayLiteral</code></a>
        </div></div>
</div>


<h3>Instance Variables</h3>
<div class="declaration" id="var-isempty_-bool">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-isempty_-bool">var isEmpty: Bool</a><div class="comment collapse" id="comment-var-isempty_-bool"><div class="p">
    <p>A Boolean value that indicates whether the set has no elements.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var isEmpty: Bool { get }</code>

    </div></div>
</div>



<h3>Instance Methods</h3>
<div class="declaration inherited" id="func-eqeq_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-eqeq_rhs_">func ==(<wbr>_:<wbr>rhs:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-eqeq_rhs_"><div class="p">
    <p>Returns a Boolean value indicating whether two values are equal.</p>

<p>Equality is the inverse of inequality. For any values <code>a</code> and <code>b</code>,
<code>a == b</code> implies that <code>a != b</code> is <code>false</code>.</p>

<p><strong>Parameters:</strong>
  <strong>lhs:</strong> A value to compare.
  <strong>rhs:</strong> Another value to compare.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func ==(lhs: Self, rhs: Self) -&gt; Bool</code>
    
        <h4>Declared In</h4>
        <a href="../../protocol/Equatable/"><code>Equatable</code></a>    
</div></div>
</div>
<div class="declaration" id="func-contains_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-contains_">func contains(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-contains_"><div class="p">
    <p>Returns a Boolean value that indicates whether the given element exists
in the set.</p>

<p>This example uses the <code>contains(_:)</code> method to test whether an integer is
a member of a set of prime numbers.</p>

<pre><code class="language-swift">let primes: Set = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
let x = 5
if primes.contains(x) {
    print(&quot;\(x) is prime!&quot;)
} else {
    print(&quot;\(x). Not prime.&quot;)
}
// Prints &quot;5 is prime!&quot;</code></pre>

<p><strong><code>member</code>:</strong>  An element to look for in the set.
<strong>Returns:</strong> <code>true</code> if <code>member</code> exists in the set; otherwise, <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func contains(_ member: Self.Element) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-formintersection_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-formintersection_">mutating func formIntersection(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-formintersection_"><div class="p">
    <p>Removes the elements of this set that aren&#39;t also in the given set.</p>

<p>In the following example, the elements of the <code>employees</code> set that are
not also members of the <code>neighbors</code> set are removed. In particular, the
names <code>&quot;Alicia&quot;</code>, <code>&quot;Chris&quot;</code>, and <code>&quot;Diana&quot;</code> are removed.</p>

<pre><code class="language-swift">var employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let neighbors: Set = [&quot;Bethany&quot;, &quot;Eric&quot;, &quot;Forlani&quot;, &quot;Greta&quot;]
employees.formIntersection(neighbors)
print(employees)
// Prints &quot;[&quot;Bethany&quot;, &quot;Eric&quot;]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func formIntersection(_ other: Self)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-formsymmetricdifference_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-formsymmetricdifference_">mutating func formSymmetricDifference(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-formsymmetricdifference_"><div class="p">
    <p>Removes the elements of the set that are also in the given set and adds
the members of the given set that are not already in the set.</p>

<p>In the following example, the elements of the <code>employees</code> set that are
also members of <code>neighbors</code> are removed from <code>employees</code>, while the
elements of <code>neighbors</code> that are not members of <code>employees</code> are added to
<code>employees</code>. In particular, the names <code>&quot;Alicia&quot;</code>, <code>&quot;Chris&quot;</code>, and
<code>&quot;Diana&quot;</code> are removed from <code>employees</code> while the names <code>&quot;Forlani&quot;</code> and
<code>&quot;Greta&quot;</code> are added.</p>

<pre><code class="language-swift">var employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let neighbors: Set = [&quot;Bethany&quot;, &quot;Eric&quot;, &quot;Forlani&quot;, &quot;Greta&quot;]
employees.formSymmetricDifference(neighbors)
print(employees)
// Prints &quot;[&quot;Diana&quot;, &quot;Chris&quot;, &quot;Forlani&quot;, &quot;Alicia&quot;, &quot;Greta&quot;]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func formSymmetricDifference(_ other: Self)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-formunion_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-formunion_">mutating func formUnion(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-formunion_"><div class="p">
    <p>Adds the elements of the given set to the set.</p>

<p>In the following example, the elements of the <code>visitors</code> set are added to
the <code>attendees</code> set:</p>

<pre><code class="language-swift">var attendees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;]
let visitors: Set = [&quot;Marcia&quot;, &quot;Nathaniel&quot;]
attendees.formUnion(visitors)
print(attendees)
// Prints &quot;[&quot;Diana&quot;, &quot;Nathaniel&quot;, &quot;Bethany&quot;, &quot;Alicia&quot;, &quot;Marcia&quot;]&quot;</code></pre>

<p>If the set already contains one or more elements that are also in
<code>other</code>, the existing members are kept.</p>

<pre><code class="language-swift">var initialIndices = Set(0..&lt;5)
initialIndices.formUnion([2, 3, 6, 7])
print(initialIndices)
// Prints &quot;[2, 4, 6, 7, 0, 1, 3]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func formUnion(_ other: Self)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-insert_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-insert_">mutating func insert(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-insert_"><div class="p">
    <p>Inserts the given element in the set if it is not already present.</p>

<p>If an element equal to <code>newMember</code> is already contained in the set, this
method has no effect. In this example, a new element is inserted into
<code>classDays</code>, a set of days of the week. When an existing element is
inserted, the <code>classDays</code> set does not change.</p>

<pre><code class="language-swift">enum DayOfTheWeek: Int {
    case sunday, monday, tuesday, wednesday, thursday,
        friday, saturday
}

var classDays: Set&lt;DayOfTheWeek&gt; = [.wednesday, .friday]
print(classDays.insert(.monday))
// Prints &quot;(true, .monday)&quot;
print(classDays)
// Prints &quot;[.friday, .wednesday, .monday]&quot;

print(classDays.insert(.friday))
// Prints &quot;(false, .friday)&quot;
print(classDays)
// Prints &quot;[.friday, .wednesday, .monday]&quot;</code></pre>

<p><strong><code>newMember</code>:</strong>  An element to insert into the set.
<strong>Returns:</strong> <code>(true, newMember)</code> if <code>newMember</code> was not contained in the
  set. If an element equal to <code>newMember</code> was already contained in the
  set, the method returns <code>(false, oldMember)</code>, where <code>oldMember</code> is the
  element that was equal to <code>newMember</code>. In some cases, <code>oldMember</code> may
  be distinguishable from <code>newMember</code> by identity comparison or some
  other means.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func insert(_ newMember: Self.Element) -&gt; (inserted: Bool, memberAfterInsert: Self.Element)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-intersection_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-intersection_">func intersection(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-intersection_"><div class="p">
    <p>Returns a new set with the elements that are common to both this set and
the given set.</p>

<p>In the following example, the <code>bothNeighborsAndEmployees</code> set is made up
of the elements that are in <em>both</em> the <code>employees</code> and <code>neighbors</code> sets.
Elements that are in either one or the other, but not both, are left out
of the result of the intersection.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let neighbors: Set = [&quot;Bethany&quot;, &quot;Eric&quot;, &quot;Forlani&quot;, &quot;Greta&quot;]
let bothNeighborsAndEmployees = employees.intersection(neighbors)
print(bothNeighborsAndEmployees)
// Prints &quot;[&quot;Bethany&quot;, &quot;Eric&quot;]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> A new set.</p>

<p><strong>Note:</strong> if this set and <code>other</code> contain elements that are equal but
  distinguishable (e.g. via <code>===</code>), which of these elements is present
  in the result is unspecified.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func intersection(_ other: Self) -&gt; Self</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-isdisjoint-with_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-isdisjoint-with_">func isDisjoint(<wbr>with:)</a>
        
<div class="comment collapse" id="comment-func-isdisjoint-with_"><div class="p">
    <p>Returns a Boolean value that indicates whether the set has no members in
common with the given set.</p>

<p>In the following example, the <code>employees</code> set is disjoint with the
<code>visitors</code> set because no name appears in both sets.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let visitors: Set = [&quot;Marcia&quot;, &quot;Nathaniel&quot;, &quot;Olivia&quot;]
print(employees.isDisjoint(with: visitors))
// Prints &quot;true&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> <code>true</code> if the set has no elements in common with <code>other</code>;
  otherwise, <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func isDisjoint(with other: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-issubset-of_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-issubset-of_">func isSubset(<wbr>of:)</a>
        
<div class="comment collapse" id="comment-func-issubset-of_"><div class="p">
    <p>Returns a Boolean value that indicates whether the set is a subset of
another set.</p>

<p>Set <em>A</em> is a subset of another set <em>B</em> if every member of <em>A</em> is also a
member of <em>B</em>.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let attendees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;]
print(attendees.isSubset(of: employees))
// Prints &quot;true&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> <code>true</code> if the set is a subset of <code>other</code>; otherwise, <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func isSubset(of other: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-issuperset-of_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-issuperset-of_">func isSuperset(<wbr>of:)</a>
        
<div class="comment collapse" id="comment-func-issuperset-of_"><div class="p">
    <p>Returns a Boolean value that indicates whether the set is a superset of
the given set.</p>

<p>Set <em>A</em> is a superset of another set <em>B</em> if every member of <em>B</em> is also a
member of <em>A</em>.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let attendees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;]
print(employees.isSuperset(of: attendees))
// Prints &quot;true&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> <code>true</code> if the set is a superset of <code>possibleSubset</code>;
  otherwise, <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func isSuperset(of other: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-remove_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-remove_">mutating func remove(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-remove_"><div class="p">
    <p>Removes the given element and any elements subsumed by the given element.</p>

<p><strong><code>member</code>:</strong>  The element of the set to remove.
<strong>Returns:</strong> For ordinary sets, an element equal to <code>member</code> if <code>member</code> is
  contained in the set; otherwise, <code>nil</code>. In some cases, a returned
  element may be distinguishable from <code>newMember</code> by identity comparison
  or some other means.</p>

<p>  For sets where the set type and element type are the same, like
  <code>OptionSet</code> types, this method returns any intersection between the set
  and <code>[member]</code>, or <code>nil</code> if the intersection is empty.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func remove(_ member: Self.Element) -&gt; Self.Element?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-subtract_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-subtract_">mutating func subtract(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-subtract_"><div class="p">
    <p>Removes the elements of the given set from this set.</p>

<p>In the following example, the elements of the <code>employees</code> set that are
also members of the <code>neighbors</code> set are removed. In particular, the
names <code>&quot;Bethany&quot;</code> and <code>&quot;Eric&quot;</code> are removed from <code>employees</code>.</p>

<pre><code class="language-swift">var employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let neighbors: Set = [&quot;Bethany&quot;, &quot;Eric&quot;, &quot;Forlani&quot;, &quot;Greta&quot;]
employees.subtract(neighbors)
print(employees)
// Prints &quot;[&quot;Diana&quot;, &quot;Chris&quot;, &quot;Alicia&quot;]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func subtract(_ other: Self)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-subtracting_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-subtracting_">func subtracting(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-subtracting_"><div class="p">
    <p>Returns a new set containing the elements of this set that do not occur
in the given set.</p>

<p>In the following example, the <code>nonNeighbors</code> set is made up of the
elements of the <code>employees</code> set that are not elements of <code>neighbors</code>:</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let neighbors: Set = [&quot;Bethany&quot;, &quot;Eric&quot;, &quot;Forlani&quot;, &quot;Greta&quot;]
let nonNeighbors = employees.subtracting(neighbors)
print(nonNeighbors)
// Prints &quot;[&quot;Diana&quot;, &quot;Chris&quot;, &quot;Alicia&quot;]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> A new set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func subtracting(_ other: Self) -&gt; Self</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-symmetricdifference_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-symmetricdifference_">func symmetricDifference(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-symmetricdifference_"><div class="p">
    <p>Returns a new set with the elements that are either in this set or in the
given set, but not in both.</p>

<p>In the following example, the <code>eitherNeighborsOrEmployees</code> set is made up
of the elements of the <code>employees</code> and <code>neighbors</code> sets that are not in
both <code>employees</code> <em>and</em> <code>neighbors</code>. In particular, the names <code>&quot;Bethany&quot;</code>
and <code>&quot;Eric&quot;</code> do not appear in <code>eitherNeighborsOrEmployees</code>.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let neighbors: Set = [&quot;Bethany&quot;, &quot;Eric&quot;, &quot;Forlani&quot;]
let eitherNeighborsOrEmployees = employees.symmetricDifference(neighbors)
print(eitherNeighborsOrEmployees)
// Prints &quot;[&quot;Diana&quot;, &quot;Forlani&quot;, &quot;Alicia&quot;]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> A new set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func symmetricDifference(_ other: Self) -&gt; Self</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-union_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-union_">func union(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-union_"><div class="p">
    <p>Returns a new set with the elements of both this and the given set.</p>

<p>In the following example, the <code>attendeesAndVisitors</code> set is made up
of the elements of the <code>attendees</code> and <code>visitors</code> sets:</p>

<pre><code class="language-swift">let attendees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;]
let visitors = [&quot;Marcia&quot;, &quot;Nathaniel&quot;]
let attendeesAndVisitors = attendees.union(visitors)
print(attendeesAndVisitors)
// Prints &quot;[&quot;Diana&quot;, &quot;Nathaniel&quot;, &quot;Bethany&quot;, &quot;Alicia&quot;, &quot;Marcia&quot;]&quot;</code></pre>

<p>If the set already contains one or more elements that are also in
<code>other</code>, the existing members are kept.</p>

<pre><code class="language-swift">let initialIndices = Set(0..&lt;5)
let expandedIndices = initialIndices.union([2, 3, 6, 7])
print(expandedIndices)
// Prints &quot;[2, 4, 6, 7, 0, 1, 3]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> A new set with the unique elements of this set and <code>other</code>.</p>

<p><strong>Note:</strong> if this set and <code>other</code> contain elements that are equal but
  distinguishable (e.g. via <code>===</code>), which of these elements is present
  in the result is unspecified.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func union(_ other: Self) -&gt; Self</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-update-with_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-update-with_">mutating func update(<wbr>with:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-update-with_"><div class="p">
    <p>Inserts the given element into the set unconditionally.</p>

<p>If an element equal to <code>newMember</code> is already contained in the set,
<code>newMember</code> replaces the existing element. In this example, an existing
element is inserted into <code>classDays</code>, a set of days of the week.</p>

<pre><code class="language-swift">enum DayOfTheWeek: Int {
    case sunday, monday, tuesday, wednesday, thursday,
        friday, saturday
}

var classDays: Set&lt;DayOfTheWeek&gt; = [.monday, .wednesday, .friday]
print(classDays.update(with: .monday))
// Prints &quot;Optional(.monday)&quot;</code></pre>

<p><strong><code>newMember</code>:</strong>  An element to insert into the set.
<strong>Returns:</strong> For ordinary sets, an element equal to <code>newMember</code> if the set
  already contained such a member; otherwise, <code>nil</code>. In some cases, the
  returned element may be distinguishable from <code>newMember</code> by identity
  comparison or some other means.</p>

<p>  For sets where the set type and element type are the same, like
  <code>OptionSet</code> types, this method returns any intersection between the 
  set and <code>[newMember]</code>, or <code>nil</code> if the intersection is empty.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func update(with newMember: Self.Element) -&gt; Self.Element?</code>
    
    
</div></div>
</div>


<h3>Default Implementations</h3>




<div class="declaration" id="-init_">
<a class="toggle-link" data-toggle="collapse" href="#comment--init_">init(<wbr>_:)</a><div class="comment collapse" id="comment--init_"><div class="p">
    <p>Creates a new set from a finite sequence of items.</p>

<p>Use this initializer to create a new set from an existing sequence, like
an array or a range:</p>

<pre><code class="language-swift">let validIndices = Set(0..&lt;7).subtracting([2, 4, 5])
print(validIndices)
// Prints &quot;[6, 0, 1, 3]&quot;</code></pre>

<p><strong><code>sequence</code>:</strong>  The elements to use as members of the new set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init&lt;S : Sequence where S.Iterator.Element == Element&gt;(_ sequence: S)</code>

    </div></div>
</div>
<div class="declaration" id="-init-arrayliteral_">
<a class="toggle-link" data-toggle="collapse" href="#comment--init-arrayliteral_">init(<wbr>arrayLiteral:)</a><div class="comment collapse" id="comment--init-arrayliteral_"><div class="p">
    <p>Creates a set containing the elements of the given array literal.</p>

<p>Do not call this initializer directly. It is used by the compiler when
you use an array literal. Instead, create a new set using an array
literal as its value by enclosing a comma-separated list of values in
square brackets. You can use an array literal anywhere a set is expected
by the type context.</p>

<p>Here, a set of strings is created from an array literal holding only
strings:</p>

<pre><code class="language-swift">let ingredients: Set = [&quot;cocoa beans&quot;, &quot;sugar&quot;, &quot;cocoa butter&quot;, &quot;salt&quot;]
if ingredients.isSuperset(of: [&quot;sugar&quot;, &quot;salt&quot;]) {
    print(&quot;Whatever it is, it&#39;s bound to be delicious!&quot;)
}
// Prints &quot;Whatever it is, it&#39;s bound to be delicious!&quot;</code></pre>

<p><strong><code>arrayLiteral</code>:</strong>  A list of elements of the new set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(arrayLiteral: Self.Element...)</code>

    </div></div>
</div>

<div class="declaration" id="-var-isempty_-bool">
<a class="toggle-link" data-toggle="collapse" href="#comment--var-isempty_-bool">var isEmpty: Bool</a><div class="comment collapse" id="comment--var-isempty_-bool"><div class="p">
    <p>A Boolean value that indicates whether the set has no elements.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var isEmpty: Bool { get }</code>

    </div></div>
</div>


<div class="declaration" id="func--isdisjoint-with_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--isdisjoint-with_">func isDisjoint(<wbr>with:)</a>
        
<div class="comment collapse" id="comment-func--isdisjoint-with_"><div class="p">
    <p>Returns a Boolean value that indicates whether the set has no members in
common with the given set.</p>

<p>In the following example, the <code>employees</code> set is disjoint with the
<code>visitors</code> set because no name appears in both sets.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let visitors: Set = [&quot;Marcia&quot;, &quot;Nathaniel&quot;, &quot;Olivia&quot;]
print(employees.isDisjoint(with: visitors))
// Prints &quot;true&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> <code>true</code> if the set has no elements in common with <code>other</code>;
  otherwise, <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func isDisjoint(with other: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--isstrictsubset-of_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--isstrictsubset-of_">func isStrictSubset(<wbr>of:)</a>
        
<div class="comment collapse" id="comment-func--isstrictsubset-of_"><div class="p">
    <p>Returns a Boolean value that indicates whether this set is a strict
subset of the given set.</p>

<p>Set <em>A</em> is a strict subset of another set <em>B</em> if every member of <em>A</em> is
also a member of <em>B</em> and <em>B</em> contains at least one element that is not a
member of <em>A</em>.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let attendees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;]
print(attendees.isStrictSubset(of: employees))
// Prints &quot;true&quot;

// A set is never a strict subset of itself:
print(attendees.isStrictSubset(of: attendees))
// Prints &quot;false&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> <code>true</code> if the set is a strict subset of <code>other</code>; otherwise,
  <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func isStrictSubset(of other: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--isstrictsuperset-of_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--isstrictsuperset-of_">func isStrictSuperset(<wbr>of:)</a>
        
<div class="comment collapse" id="comment-func--isstrictsuperset-of_"><div class="p">
    <p>Returns a Boolean value that indicates whether this set is a strict
superset of the given set.</p>

<p>Set <em>A</em> is a strict superset of another set <em>B</em> if every member of <em>B</em> is
also a member of <em>A</em> and <em>A</em> contains at least one element that is <em>not</em>
a member of <em>B</em>.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let attendees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;]
print(employees.isStrictSuperset(of: attendees))
// Prints &quot;true&quot;

// A set is never a strict superset of itself:
print(employees.isStrictSuperset(of: employees))
// Prints &quot;false&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> <code>true</code> if the set is a strict superset of <code>other</code>; otherwise,
  <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func isStrictSuperset(of other: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--issubset-of_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--issubset-of_">func isSubset(<wbr>of:)</a>
        
<div class="comment collapse" id="comment-func--issubset-of_"><div class="p">
    <p>Returns a Boolean value that indicates whether the set is a subset of
another set.</p>

<p>Set <em>A</em> is a subset of another set <em>B</em> if every member of <em>A</em> is also a
member of <em>B</em>.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let attendees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;]
print(attendees.isSubset(of: employees))
// Prints &quot;true&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> <code>true</code> if the set is a subset of <code>other</code>; otherwise, <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func isSubset(of other: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--issuperset-of_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--issuperset-of_">func isSuperset(<wbr>of:)</a>
        
<div class="comment collapse" id="comment-func--issuperset-of_"><div class="p">
    <p>Returns a Boolean value that indicates whether the set is a superset of
the given set.</p>

<p>Set <em>A</em> is a superset of another set <em>B</em> if every member of <em>B</em> is also a
member of <em>A</em>.</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let attendees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Diana&quot;]
print(employees.isSuperset(of: attendees))
// Prints &quot;true&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> <code>true</code> if the set is a superset of <code>other</code>; otherwise,
  <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func isSuperset(of other: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--subtract_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--subtract_">mutating func subtract(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func--subtract_"><div class="p">
    <p>Removes the elements of the given set from this set.</p>

<p>In the following example, the elements of the <code>employees</code> set that are
also members of the <code>neighbors</code> set are removed. In particular, the
names <code>&quot;Bethany&quot;</code> and <code>&quot;Eric&quot;</code> are removed from <code>employees</code>.</p>

<pre><code class="language-swift">var employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let neighbors: Set = [&quot;Bethany&quot;, &quot;Eric&quot;, &quot;Forlani&quot;, &quot;Greta&quot;]
employees.subtract(neighbors)
print(employees)
// Prints &quot;[&quot;Diana&quot;, &quot;Chris&quot;, &quot;Alicia&quot;]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func subtract(_ other: Self)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--subtracting_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--subtracting_">func subtracting(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func--subtracting_"><div class="p">
    <p>Returns a new set containing the elements of this set that do not occur
in the given set.</p>

<p>In the following example, the <code>nonNeighbors</code> set is made up of the
elements of the <code>employees</code> set that are not elements of <code>neighbors</code>:</p>

<pre><code class="language-swift">let employees: Set = [&quot;Alicia&quot;, &quot;Bethany&quot;, &quot;Chris&quot;, &quot;Diana&quot;, &quot;Eric&quot;]
let neighbors: Set = [&quot;Bethany&quot;, &quot;Eric&quot;, &quot;Forlani&quot;, &quot;Greta&quot;]
let nonNeighbors = employees.subtract(neighbors)
print(nonNeighbors)
// Prints &quot;[&quot;Diana&quot;, &quot;Chris&quot;, &quot;Alicia&quot;]&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A set of the same type as the current set.
<strong>Returns:</strong> A new set.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func subtracting(_ other: Self) -&gt; Self</code>
    
    
</div></div>
</div>





